//index.js
Page({
  data: {
    scrollViewHeight: 200,
    dateOfToday: "",
    curTime: "",
    error:"",
    items:[]
   
  },
  onShareAppMessage: function () {

    return {

      title: '历史上的今天',

      desc: '看看今天发生在历史上的新奇事儿',

      path: '/pages/index/index'

    }

  },

  refreshTime: function () {
    var now = new Date();
    var h = now.getHours();
    var m = now.getMinutes();
    var s = now.getSeconds();
    var time = h + ':' + m + ':' + s;
    //改变变量的值
    this.setData({
      curTime: time
    });
  },

  onLoad: function () {
    var that = this;

    //显示加载动画
    wx.showLoading({
      title: '正在加载',
    });

    //获取屏幕高度
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.windowHeight);
        that.setData({
          scrollViewHeight: res.windowHeight - 100
        });
      }
    });

    //获取当前日期
    var myDate = new Date();
    // var dateStr = myDate.getFullYear() + '年' + (myDate.getMonth() + 1) + '月' + myDate.getDate() + '日';
    var dateStr = (myDate.getMonth() + 1) + '月' + myDate.getDate() + '日';
    this.setData({
      dateOfToday: "历史上的："+dateStr
    });

    //发送请求
    wx.request({
      url: "https://api.ooopn.com/history/api.php?type=json",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      success: function (res) {
        //处理api返回的数据
        var  msg = res.data;
        console.log('begin');
        // console.log(msg);
        console.log("success:" + res.data.code);
        var ayy = msg['content'];
        try {
          that.setData({
            items: ayy,
            //dateOfToday: msg['day']
          });
         
        } catch (e) {
          that.setData({
            items: ['网络故障，请检查网络']
          });
        }
        wx.hideLoading();
      },
      fail: function (err) {
        console.log("error:" + err);
        that.setData({
          error: "opps'.....网络故障，请检查网络"
        });
        wx.hideLoading();
      }
    });

  },
  viewDetail: function (e) {
    var title = e.currentTarget.dataset.title;
    var year = e.currentTarget.dataset.year;
    var url = '../detail/detail?title=' + title + '&year=' + year + '&date=' + this.data.dateOfToday;

    wx.navigateTo({
      url: url
    })
  },
})
